package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class LocalCommonProvinceHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        KiteAttr provinceAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10053");
        //LocalAttrHelper.initPlatformProvince(provinceAttr, kiteProdInsts.get(0).getProdId());
        operateSceneInstService.setCommonAttr(sceneInstId, provinceAttr);
        if(KiteStringUtils.isNotEmpty(value)){
            if(!KiteStringUtils.equals(value, provinceAttr.getValue())){
                if(!(provinceAttr.getPreValue() == null && KiteStringUtils.equals(value, provinceAttr.getOriginValue()))) {
                    KiteAttr cityAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10054");
                    KiteAttr countyAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10055");
                    if (!Objects.isNull(cityAttr)) {
                        cityAttr.setValue("");
                        cityAttr.setValueDesc("");
                        operateSceneInstService.setCommonAttr(sceneInstId, cityAttr);
                    }
                    if (!Objects.isNull(countyAttr)) {
                        countyAttr.setValue("");
                        countyAttr.setValueDesc("");
                        operateSceneInstService.setCommonAttr(sceneInstId, countyAttr);
                    }
                }
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
